<?php
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/23
 *  use service\HttpService;
 *  return HttpService::getRequest("URL",$param);
 */
namespace base;
use think\Controller;
use think\Db;
use think\facade\Log;
use base\map\Maps;

class BaseMethod extends Controller {
	
	//新令牌
    private  $token;
	
	/**
    * 构造函数自动执行
    * @return null
    */
    public function __construct($token=null){
    	$this ->token =  $token;
    }

	/**
     * 获取示例
     * @param array $options 实例配置
     * @return static
     */
    public  function instance($token=null)
    {
        return new self($token);
    }
	
	/**
    * 返回操作成功json
    * @param string $msg
    * @param string $url
    * @param array $data
    * @return array
    */
	public function ReSucess ( $data = "", $msg = "操作成功", $code = 0 ){
		$array = array();
		$array["code"] = $code;
		$array["data"] = $data;
		$array["msg"] = $msg;
		$module = request()->module();
		$method = request()->method();
		if($module == 'commons' && !request()->isAjax() && $method != 'POST'){
			echo json_encode($array);
			return true;
		}
		self::addLog($array);
		if( request()->isAjax() or $module != 'admin' or $method == 'POST'){
			echo json_encode($array); 
			die;			
		}
		return json($array);
	}
	
	/**
    * 返回操作失败json
    * @param string $msg
    * @param string $title
    * @param array $data
    * @return array
    */
	public  function ReError ( $msg = "错误！",$code=-1, $data=[]){
		$array = array();
		$array["code"] = $code;
		$array["data"] = $data;
		$array["msg"] = $msg;
		self::addLog($array);
		if( request()->isAjax() or request()->module() != 'admin' ){
			echo json_encode($array); die;
		}
		echo $array["msg"]; die;
	}
	
	//添加日志
	public  function addLog($array){
		$request = request();
		$header = $request->header();
		$http = $request->server('HTTP_REFERER');
		if( strstr(strtolower( get_active_url() ),"/msg/")  ){
			return;
		}
        if( strstr(strtolower( get_active_url() ),"install/create")   ){
            return;
        }
        if( strstr(strtolower( get_active_url() ),"v3")   ){
            return;
        }
		if( !strstr($http,"/system/logMng/index") ){
			$strjson = json_encode($array,JSON_UNESCAPED_UNICODE);
			$userInfo = $request->userInfo;
			$data['ip'] = $ip = $request->ip();
			$data['iparea'] = Maps::getIpArea($ip);
			$data['referer'] = $request->server('HTTP_REFERER');
			$data['cookie'] = !empty($header['cookie']) ? $header['cookie'] :'';
			$data['guid'] = $userInfo['guid'] ?? $userInfo['user_guid'] ?? $userInfo['id'];
			$data['userinfo'] =  $request->userInfo;
			$data['url'] = strtolower( get_active_url() );
			$param = array_merge($request->get(),$request->post());
			$data['param'] = json_encode( $param ,JSON_UNESCAPED_UNICODE);
			$data['result'] = $array["code"] == 0  ?  "成功：".$strjson : "错误：".$strjson;
			$referer = str_replace(APP_PATH,'', empty($header['referer']) ? '' : $header['referer']);
			if( $referer != '/system/logMng/index' ){
				try {
					Db::listen(function ($sql, $time, $explain) { });
			    	Db::name('system_log') ->whereTime('create_time', '<', '-12 days')->delete();
					Db::name('system_log') ->strict(false)-> insert($data);
				} catch (\Exception $e) {
	
				}
			}
		}
	}

}